package hudson.plugins.aspen;


import java.io.File;
import java.io.IOException;

import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Descriptor.FormException;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;

import net.sf.json.JSONObject;

import org.kohsuke.stapler.StaplerRequest;

public class AspenPublisher extends Recorder{

	private String composite;
	private String logsPattern;
	
	public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
		listener.getLogger().println("[Aspen] Publishing Report...");
		File compositeReportFile = new File(composite);
		AspenBuildAction buildAction = new AspenBuildAction(build);
		
		if(compositeReportFile.exists()){
			
			FeatureSupportReport featureSupport = new FeatureSupportReport(compositeReportFile);
			listener.getLogger().println( 
				"[Aspen] Reporting on " +
				featureSupport.getFeatures().size() +
				" Features and " +
				featureSupport.getPlatforms().size()+
				" Platforms."
			);
			listener.getLogger().println(
				"[Aspen] Passed: " +
				featureSupport.getPassed() +
				" Failed: " +
				featureSupport.getFailed()
			);
			buildAction.setFeatureSupport(featureSupport);
		} else {
			listener.getLogger().println("File: \"" + compositeReportFile.getAbsolutePath() + "\" could not be found."); 
			throw new IOException("File: \"" + compositeReportFile.getAbsolutePath() + "\" could not be found.");
		}
		
		build.addAction(buildAction);
		return true;
	}
	
	@Override
    public Action getProjectAction(AbstractProject<?, ?> project) {
        return new AspenProjectAction(project);
    }
	
	public BuildStepMonitor getRequiredMonitorService() {
        return BuildStepMonitor.BUILD;
    }
	
	@Override
    public BuildStepDescriptor<Publisher> getDescriptor() {
        return DESCRIPTOR;
    }
	
	@Extension
    public static final BuildStepDescriptor<Publisher> DESCRIPTOR = new DescriptorImpl();

    public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {

    	public DescriptorImpl() {
            super(AspenPublisher.class);
        }
    	
		@SuppressWarnings("unchecked")
		@Override
		public boolean isApplicable(Class<? extends AbstractProject> proj) {
			return true;
		}

		@Override
		public String getDisplayName() {
			return "Aspen Reporting";
		}
    	
		@Override
        public String getHelpFile() {
            return "/plugin/aspen/help.html";
        }

        @Override
        public Publisher newInstance(StaplerRequest req, JSONObject json) throws FormException {
        	AspenPublisher pub = new AspenPublisher();
        	req.bindParameters(pub, "aspen.");
        	return pub;
        }
    
    }

	public String getComposite() {
		return composite;
	}

	public void setComposite(String composite) {
		this.composite = composite;
	}

	public String getLogsPattern() {
		return logsPattern;
	}

	public void setLogsPattern(String logsPattern) {
		this.logsPattern = logsPattern;
	}

}
